<template>
	<view class="goods-list-container">
        <view class="goods-list" v-for="(item,i) in goodsList" :key='i'>
            
            <goods-detail :goods='item' @click='gotoDetail(item)'></goods-detail>
            
          </view>
        </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
        // 定义数据请求参数
        queryObj:{
          query:'',
          cid:'',
          pagenum: 1,
          pagesize: 10,
        },
        total:0,
				goodsList:[],
        defaultPic:"http://image2.suning.cn/uimg/b2c/newcatentries/0000000000-000000000168415104_1_400x400.jpg",
        flag:false
			};
		},
    onLoad(options) {
      this.queryObj.query = options.query || ''
      console.log(options,'options',this.queryObj.query)
      this.queryObj.cid = options.cid || ''
      this.getGoodsList()
    },
    onReachBottom() {
      // 设置节流阀
      if(this.flag) return uni.$showMsg('数据正在加载中！1111111111111111')
      if(this.queryObj.pagesize * this.queryObj.pagenum >= this.total) return uni.$showMsg('没有更多的商品了！')
      this.queryObj.pagenum += 1
      this.getGoodsList()
    },
    onPullDownRefresh(){
      this.pagenum = 1
      this.total = 0
      this.goodsList = []
      this.flag = false
      this.getGoodsList(() => uni.stopPullDownRefresh())
    },
    methods:{
      async getGoodsList(cb){
        this.flag = true
        const {data : res} = await uni.$http.get('/goods/search', this.queryObj)
        
        this.flag = false
        cb && cb()
        // console.log(res,'!!!!!!!!!!!')
        if(res.meta.status !== 200) return uni.$showMsg()
        // console.log(res.message.goods)
        this.goodsList = [...this.goodsList,...res.message.goods]
        this.total = res.message.total
      },
      gotoDetail(item){
        uni.navigateTo({
          url:"/subpkg/goods_detail/goods_detail?goods_id=" + item.goods_id
        })
      }
    }
	}
</script>

<style lang="scss">
.goods-list{
  .goods-item{
    display: flex;
    border-bottom: 1px solid #efefef;
    .left{
      width: 300rpx;
      margin-right: 20rpx;
      image{
        width: 300rpx;
      }
 
    }
    
    .right{
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      padding: 20rpx 0;
      .content{
        padding-top: 10rpx;
      }
      .price{
        color: #c00000;
      }
    }
    
  }
}
</style>
